package com.kissoft.mgr.ctrler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.kissoft.common.util.KisLogger.LoggerCode;
import com.kissoft.mgr.service.AuthService;

@Controller
@RequestMapping("/mgr/login/login.do")
public class AuthCtrler extends Common{
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView loginForm(HttpServletRequest request) {

		String view = "/mgr/login/login";
		ModelAndView mav = new ModelAndView();
		mav.setViewName(view);

		return mav;
	}
/*	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView loginProcess(
			@RequestParam(value="adminEmail", required=true ) String adminEmail,
			@RequestParam(value="adminPasswd", required=true ) String adminPasswd,
			HttpSession session,
			HttpServletResponse response
			
			) {
		
		String view = "/mgr/result/result";
		logger.logWrite(LoggerCode.INFO, "adminEmail : " + adminEmail);
		logger.logWrite(LoggerCode.INFO, "adminPasswd : " + adminPasswd);
		
		Map<String,String> paramMap = new HashMap<String,String>();
		paramMap.put("adminEmail", adminEmail);
		paramMap.put("adminPasswd", adminPasswd);
		
		AdminDto admin = null;
		boolean isSuccess = false;
		try {
			//로그인 체크
			admin = authService.getAdminMember(paramMap);
			if(admin != null)	//로그인에 성공하면
			{
				//환경설정에서 세션 유지 시간 가져온다.
				//Integer sessionDuration = configService.getSessionDuration();
				String sessionDuration = config.getConfigValue(this.configService,"SESSIONDURATION");
				//세션 아이디 생성
				String sessionId = session.getId();

				paramMap.put("sessionDuration", sessionDuration);
				paramMap.put("sessionId", sessionId);
				//이전 세션 삭제
				boolean delRtnValue = authService.delSession(paramMap);
				//세션 생성
				boolean rtnValue = authService.setSession(paramMap);
				if(delRtnValue && rtnValue)//로그인 및 세션 성공 쿠키를 굽자.
				{
					//쿠키 굽자.
					response.addCookie(util.makeCookis("sessionId", sessionId, Integer.parseInt(sessionDuration,10)));
					response.addCookie(util.makeCookis("email", adminEmail, Integer.parseInt(sessionDuration,10)));
					
				}
			}
			isSuccess = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("isSuccess", isSuccess);
		jsonObj.put("result", JSONSerializer.toJSON(admin));
		JSONObject resultObj = JSONObject.fromObject(jsonObj);
		
		logger.logWrite(LoggerCode.INFO, "START");
		logger.logWrite(LoggerCode.INFO, resultObj.toString());
		logger.logWrite(LoggerCode.INFO, "END");
		
		ModelAndView mav = new ModelAndView();
		mav.addObject("json", resultObj.toString());
		mav.setViewName(view);
		
		return mav;
	}*/

}
